require("EntitiesView")
require("LocationsView")
require("PlayerView")
require("MindsView")
require("LogView")

local printBackup = print
require("wx")
print = printBackup

MainDevView = class("MainDevView")

---

function MainDevView:initialize()

	gView = self
	
	local size = wx.wxGetDisplaySize()
	size:SetWidth(size:GetWidth() / 2)
	size:SetHeight(size:GetHeight() - 40)
	
    self.frame = wx.wxFrame( 
    	wx.NULL, 
    	wx.wxID_ANY, 
    	"Kabbale",
        wx.wxPoint(size:GetWidth(), 0), 
        size,
        wx.wxDEFAULT_FRAME_STYLE 
	)

	self.views = {}
	self.buttonDescriptors = {}
	
	self:addView( EntitiesView:new(), false )
	self:addView( LocationsView:new(), false )
	self:addView( MindsView:new(), false )
	self:addView( PlayerView:new(), true )
	self:addView( LogView:new(), false )

	self:layoutViews()
	
	self.frame:Connect(wx.wxID_ANY, wx.wxEVT_COMMAND_BUTTON_CLICKED,
		
	    function(event)
	    
	    	local buttonDescriptor = gView.buttonDescriptors[event:GetId()]
	    	buttonDescriptor.view:onButtonClick(buttonDescriptor.descriptor)
	    	
	    	gGame:onBeat()
	    end
	)	
	
	self.frame:Connect(wx.wxID_ANY, wx.wxEVT_CLOSE_WINDOW ,
		
	    function(event)
	    
			gView.frame:Destroy()
			gMonitor:onApplicationExit()
	    end
	)	
end

---

function MainDevView:loop()

	wx.wxGetApp():MainLoop()
end

---

function MainDevView:addView(view, destroyWindowAtUpdate)

	self.views[#self.views + 1] = {view = view, destroyWindowAtUpdate = destroyWindowAtUpdate}
end

---

function MainDevView:layoutViews()

    self.rootSizer = wx.wxBoxSizer( wx.wxHORIZONTAL )
    
    for i, viewDescriptor in ipairs(self.views) do
    
		self.rootSizer:Add(
	     	viewDescriptor.view.rootWindow,
	     	1,           
	     	wx.wxALL + wx.wxEXPAND,     
	     	2
		)
    end
    
    self.frame:SetSizer( self.rootSizer )
    self.frame:Layout()
    self.frame:Show(true)
end

---

function MainDevView:update()

	self.frame:Freeze()

    for i, viewDescriptor in ipairs(self.views) do
    
   		if 
   			viewDescriptor.view.rootWindow and
   			viewDescriptor.destroyWindowAtUpdate
   		then
			viewDescriptor.view.rootWindow:Destroy()    		
		end
		
    	viewDescriptor.view:update()
    end
    
    self:layoutViews()
    self.frame:Thaw()
end